package com.example.administrator.mysaft;

import android.app.ActivityManager;
import android.content.Context;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.MemoryInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import android.app.ActivityManager.RunningAppProcessInfo;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 判断服务是否处于运行状态
 * Created by Administrator on 2017/6/22.
 */

public class SystemInfosUtils {
    public static boolean isServiceRunning(Context context,String className){
        ActivityManager am= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> infos=am.getRunningServices(200);
        for (RunningServiceInfo info:infos){
            String serviceClassName=info.service.getClassName();
            if (className.equals(serviceClassName)){
                return true;
            }
        }
        return false;
    }
//  获取手机总内存大小单位
    public  static long getToalMen(){
        try {
            FileInputStream fis=new FileInputStream(new File("/proc/meminfo"));
            BufferedReader br=new BufferedReader(new InputStreamReader(fis));
            String totalInfo=br.readLine();
            StringBuffer sb=new StringBuffer();
            for (char c:totalInfo.toCharArray()){
                if (c>='0'&& c<='9'){
                    sb.append(c);
                }
            }
            long bytesize=Long.parseLong(sb.toString())*1024;
            return bytesize;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
//    获取内存信息
    public static long getAvailMem(Context context){
        ActivityManager am= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//获取内存的大小
        MemoryInfo outInfo=new ActivityManager.MemoryInfo();
        am.getMemoryInfo(outInfo);
        long availMem=outInfo.availMem;
        return availMem;

    }
//    得到正在运行的进程数量
    public static int getRunningPocessCount(Context context){
        ActivityManager am= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> runningAppProcessInfos=am.getRunningAppProcesses();
        int count=runningAppProcessInfos.size();
        return count;
    }
}
